package com.mccken.practice.top100.Top_1$20.top_3;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * 最长连续序列（不要求顺序）
 *
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-12-28 12:16
 **/
public class LongestConsecutive {

	public int longestConsecutive(int[] nums) {
		// 先去重
		Set<Integer> num_set = new HashSet<>();
		for (int num : nums) {
			num_set.add(num);
		}

		// 最长连续值
		int longestStreak = 0;

		for (Integer num : num_set) {
			if (!num_set.contains(num - 1)) {
				int currentNum = num;
				// 当前连续值
				int currentStreak = 1;
				while (num_set.contains(currentNum + 1)) {
					currentStreak++;
					currentNum++;
				}

				longestStreak = Math.max(longestStreak, currentStreak);
			}
		}

		return longestStreak;
	}

	public static void main(String[] args) {
		int[] nums = {0, 1, 0, 3, 12};
		int count = 0;
		Queue<Integer> q = new LinkedList<>();
		for (int i = 0; i < nums.length; i++) {
			if (nums[i] != 0) {
				q.offer(nums[i]);
			}
		}
		for (int i = 0; i < nums.length; i++) {
			if (!q.isEmpty()) {
				nums[i] = q.poll();
				continue;
			}
			nums[i] = 0;
		}

		System.out.println();
	}

}
